﻿using System.Linq;
using Ayatta.Contract.Domain;
using Dapper;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Collections.Generic;

namespace Ayatta.Service
{
    public abstract class BaseService
    {
        #region ConnectionManager

        private static readonly IDictionary<string, string> SqlConnectionStringDic = new Dictionary<string, string>();

        protected internal IDbConnection GetConnection(string name)
        {
            string connectionstring;
            if (SqlConnectionStringDic.ContainsKey(name))
            {
                connectionstring = SqlConnectionStringDic[name];
            }
            else
            {
                connectionstring = ConfigurationManager.ConnectionStrings[name].ConnectionString;
                SqlConnectionStringDic[name] = connectionstring;
            }
            return new SqlConnection(connectionstring);
        }

        protected void PurgeCache()
        {
            SqlMapper.PurgeQueryCache();
        }

        protected internal PagedList<Magic<string, string, int>> GetPagedCache(int page = 1, int size = 50,
                                                                                int hitCount = 0)
        {
            var skip = (page - 1) * size;
            var count = SqlMapper.GetCachedSQLCount();
            var list = SqlMapper.GetCachedSQL().Where(o => o.Item3 >= hitCount).Skip(skip)
                .Take(size).Select(o => new Magic<string, string, int>(o.Item1, o.Item2, o.Item3)).ToList();
            return new PagedList<Magic<string, string, int>>(list, count);
        }

        #endregion
        internal static class ConnectionName
        {
            internal static readonly string Infrastructure = "AyattaInfrastructure";
            internal static readonly string Main = "SilenthinkMain";
            internal static readonly string Extra = "AyattaExtra";
            internal static readonly string Store = "AyattaStore";
            internal static readonly string Trade = "AyattaTrade";
            internal static readonly string Game = "SilenthinkGame";
            internal static readonly string Passport = "AyattaPassport";
            internal static readonly string Account = "AyattaAccount";

            internal static readonly string Auction = "SilenthinkAuction";
            internal static readonly string Log = "SilenthinkLog";

        }

        internal static class Database
        {
            internal static class Passport
            {
                internal static class Table
                {
                    internal static readonly string User = "[User]";
                    internal static readonly string UserExtra = "[User.Extra]";
                    internal static readonly string UserProfile = "[User.Profile]";
                    internal static readonly string UserAddress = "[User.Address]";
                    internal static readonly string UserInvoice = "[User.Invoice]";

                    internal static readonly string PreUser = "[Pre.User]";

                    internal static readonly string OAuth = "[OAuth]";
                    internal static readonly string OAuthUser = "[OAuth.User]";

                }
            }
            internal static class Account
            {
                internal static readonly string Name = "[AyattaAccount]";
                internal static class Table
                {
                    internal static readonly string Account = "[dbo].[Account]";
                    internal static readonly string AccountRecharge = "[dbo].[Account.Recharge]";
                    internal static readonly string AccountPayment = "[dbo].[Account.Payment]";
                    internal static readonly string PaymentPlatform = "[dbo].[Payment.Platform]";
                    internal static readonly string PaymentBank = "[dbo].[Payment.Bank]";
                    internal static readonly string PaymentEBank = "[dbo].[Payment.EBank]";
                    internal static readonly string RechargePackage = "[dbo].[Recharge.Package]";
                    internal static readonly string RechargePackageRequirement = "[dbo].[Recharge.Package.Requirement]";
                    internal static readonly string AccountPaymentLog = "[Account.Payment.Log]";

                    internal static readonly string AccountDetailMoney = "[Account.Detail.Money]";
                    internal static readonly string AccountDetailCoin = "[Account.Detail.Coin]";
                    internal static readonly string AccountDetailIntegral = "[Account.Detail.Integral]";
                    internal static readonly string AccountDetailGame = "[Account.Detail.Game]";
                }
            }

            internal static class Payment
            {
                internal static class Table
                {
                    internal static readonly string UserAccount = "[User.Account]";
                    internal static readonly string UserTrade = "[User.Trade]";
                    internal static readonly string UserRecharge = "[User.Recharge]";

                    internal static readonly string RechargePackage = "[Recharge.Package]";
                    internal static readonly string RechargePackageRequirement = "[Recharge.Package.Requirement]";

                    internal static readonly string PayTrade = "[Pay.Trade]";

                    internal static readonly string UserIntegral = "[User.Currency.Integral]";
                    internal static readonly string PaymentBank = "[Payment.Bank]";
                    internal static readonly string PaymentEBank = "[Payment.EBank]";
                    internal static readonly string PaymentPlatform = "[Payment.Platform]";
                }

            }
            internal static class Auction
            {
                internal static class Table
                {
                    internal static readonly string Auction = "[Auction]";
                    internal static readonly string Bidding = "[Auction.Bidding]";
                    internal static readonly string AuctionOrder = "[Auction.Order]";
                    internal static readonly string AuctionShare = "[Auction.Share]";
                    internal static readonly string AuctionRobot = "[Auction.Robot]";
                }
            }

            internal static class Infrastructure
            {
                internal static class Table
                {
                    internal static readonly string Question = "[Question]";
                    internal static readonly string Area = "[Area]";
                    internal static readonly string EmailAccount = "[Email.Account]";
                    internal static readonly string EmailTemplate = "[Email.Template]";
                    internal static readonly string EmailQueue = "[Email.Queue]";
                    internal static readonly string LogisticsCompany = "[Logistics.Company]";
                    internal static readonly string Slide = "[Slide]";
                    internal static readonly string SlideCategory = "[Slide.Category]";
                }

            }
            internal static class Order
            {
                internal static class Table
                {
                    internal static readonly string Order = "[Order]";
                    internal static readonly string OrderExtra = "[Order.Extra]";
                    internal static readonly string OrderItem = "[Order.Item]";
                    internal static readonly string OrderExchangeDetail = "[Order.Exchange.Detail]";

                    internal static readonly string OrderNote = "[Order.Note]";
                    internal static readonly string Shipping = "[Shipping]";
                    internal static readonly string ShippingExtra = "[ShippingExtra]";

                    internal static readonly string Refund = "[Refund]";
                }

            }
            internal static class Trade
            {
                internal static class Table
                {
                    internal static readonly string Order = "[Order]";
                    internal static readonly string OrderExtra = "[Order.Extra]";
                    internal static readonly string OrderItem = "[Order.Item]";
                    internal static readonly string OrderExchangeDetail = "[Order.Exchange.Detail]";

                    internal static readonly string OrderNote = "[Order.Note]";
                    internal static readonly string Shipment = "[Shipment]";
                    internal static readonly string ShipmentExtra = "[Shipment.Extra]";

                    internal static readonly string Refund = "[Refund]";
                }

            }
            internal static class Game
            {
                internal static class Table
                {
                    internal static readonly string Game = "[Game]";
                    internal static readonly string GameUser = "[Game.User]";
                    internal static readonly string GameServer = "[Game.Server]";
                    internal static readonly string GameArticle = "[Game.Article]";
                    internal static readonly string GameTrial = "[Game.Trial]";
                    internal static readonly string GameTrialHistory = "[Game.Trial.History]";

                    internal static readonly string GameNoviceCard = "[Game.Novice.Card]";

                }
            }

            internal static class Store
            {
                internal static class Table
                {
                    internal static readonly string CategoryGroup = "[Category.Group]";
                    internal static readonly string ProductCategory = "[Product.Category]";
                    internal static readonly string ProductCategoryProp = "[Product.Category.Prop]";
                    internal static readonly string ProductCategoryPropValue = "[Product.Category.Prop.Value]";

                    internal static readonly string Product = "[Product]";

                    internal static readonly string ProductItem = "[Product.Item]";
                    internal static readonly string ProductItemSku = "[Product.Item.Sku]";

                    internal static readonly string ProductAuction = "[Product.Auction]";

                    /*
                    internal static readonly string ProductExtra = "[ProductExtra]";
                    internal static readonly string ProductVariant = "[ProductVariant]";

                    internal static readonly string ProductAttribute = "[ProductAttribute]";

                    internal static readonly string ProductAttributeName = "[ProductAttributeName]";
                    internal static readonly string ProductAttributeValue = "[ProductAttributeValue]";
                    */
                    internal static readonly string Discount = "[Discount]";
                    internal static readonly string DiscountRequirement = "[Discount.Requirement]";
                    internal static readonly string DiscountProductCategoryMapping = "[DiscountProductCategoryMapping]";
                    internal static readonly string DiscountItemMap = "[Discount.Item.Map]";
                    internal static readonly string DiscountUsageHistory = "[Discount.UsageHistory]";

                    internal static readonly string ProductAdvisory = "[ProductAdvisory]";
                    internal static readonly string ProductReview = "[ProductReview]";



                }
            }

            internal static class Extra
            {
                internal static class Table
                {
                    internal static readonly string AuthSms = "[Auth.Sms]";
                    internal static readonly string AuthEmail = "[Auth.Email]";
                    internal static readonly string AppException = "[App.Exception]";

                }

            }

            internal static class Log
            {
                internal static class Table
                {
                    internal static readonly string AppException = "[App.Exception]";
                }

            }
        }
    }
}